Yacon.widget.grid.list.BaseGrid = function (conText)
{
	this.elementID          = "";
	this.elementType        = "customgrid";
	this.gridType           = "read";
	this.gridcheck         = null;	
		
	Yacon.apply(this,conText);

};

Yacon.widget.grid.list.BaseGrid.Extend(Object,{
		
	baseCreate:function()
	{
		
		this.element            	= document.getElementById(this.elementID);
		if (!this.element)
			return;
		
		this.element.Control    	= this;
				
		this.headControl =  new Yacon.widget.grid.list.ListHead({parentList:this});		
		this.viewControl =  new Yacon.widget.grid.list.ListView({parentList:this});					
		if (this.isBottom())
		{
			this.bottomControl =  new Yacon.widget.grid.list.ListBottom({parentList:this});
		}
		if (!Yacon.isEmpty(this.element.getAttribute("excuteMethod")))
			this.setExcuteMethod(this.element.getAttribute("excuteMethod"));
		
		if (!Yacon.isEmpty(this.element.getAttribute("refreshMethod")))
			this.setRefreshMethod(this.element.getAttribute("refreshMethod"));
		
			
		this.convertDataset();
		
		addResizeElement(this.element);
		
		this.setWidth();
		
		this.setHeight();			
		
	},
	isBottom:function()
	{
		return Yacon.isTrue(this.element.getAttribute("isBottom"));
	},
	isEdit:function()
	{
		if (this.getGridType()=="edit")
			return true;
		else
			return false;
	},
	getGridType:function()
	{
		return this.element.getAttribute("gridType");
	},
	isCheck:function()
	{
		if (this.gridcheck == null)
		{	
			if (this.element.getAttribute("choiceType").isEqual("check"))
				this.gridcheck =true;
			else
				this.gridcheck =false;
		}
		return this.gridcheck;
	},
	getAutoSize:function()
	{
		return this.element.getAttribute("autosize");
	},
	getRightPlate:function()
	{
		var rightPlate = Yacon.getInt(this.element.getAttribute("rightPlate"));
		if (rightPlate<0)
			rightPlate =0;
		return rightPlate;
	},
	getBottomPlate:function()
	{
		var bottomPlate = Yacon.getInt(this.element.getAttribute("bottomPlate"));
		if (bottomPlate<0)
			bottomPlate =0;
		return bottomPlate;
	},
	getActiveRow:function()
	{
		return this.viewControl.activeRow;
	},
	getDataSet:function ()
	{	
		if (!this.dataSet)
		{
			var dataSetID = this.element.getAttribute("id") + "_dataSet";		
			if(this.element.getAttribute("dataSet") && this.element.getAttribute("dataSet") !="")
				 dataSetID = this.element.getAttribute("dataSet");
			
			if (dataSetID)
			{
				if (!isExitDataset(dataSetID))
				{
					this.dataSet   =  createDataset(dataSetID);	
				}else
					this.dataSet   =  getDatasetByID(dataSetID);
				
				if (this.dataSet)
				{
					this.dataSet.regTable(this);
					addElement(this.element);
					
					if (window.parameter)
					{
						for(var name in window.parameter)
						{
							if (!name.isEqual("logid"))
								this.dataSet.setForeignField(name,window.parameter[name]);
						}
					}
				}
			}
			this.dataSet.addAttribute("absolutePage","1");
			this.dataSet.addAttribute("pageSize",this.getPageSize());
		}		
		return this.dataSet;
	},
		
	getCheckDataSet:function()
	{
		return this.viewControl.getCheckDataSet();	
	},
	appendRow :function()
	{
		if (Yacon.isfireuserEvent(this.element.id + "_beforeInsert"))
		{
			if (!Yacon.fireUserEvent(this.element.id + "_beforeInsert",[this]))
				return;
		}
		
		this.viewControl.creatRow();
		
		if (Yacon.isfireuserEvent(this.element.id + "_afterInsert"))
		{
			if (!Yacon.fireUserEvent(this.element.id + "_afterInsert",[this]))
				return;
		}
	},	
	deleteRecord: function()
	{	
		if (Yacon.isfireuserEvent(this.element.id + "_beforeDelete"))
		{
			if (!Yacon.fireUserEvent(this.element.id + "_beforeDelete",[this]))
				return;
		}
		if (!this.getDataSet())
		{
			alert("列表数据集没有创建！");
			return;
		}
	
		var dataSet = this.viewControl.deleteRowByDataSet();
		if (dataSet != null)
		{	
			dataSet.post();
			this.getDataSet().refresh();		
			
			if (Yacon.isfireuserEvent(this.element.id + "_afterDelete"))
			{
				if (!Yacon.fireUserEvent(this.element.id + "_afterDelete",[this]))
					return;
			}
		}
	},
	deleteRow: function(record)
	{
		this.viewControl.deleteViewRow(record);		
	},
	clearRow: function()
	{
		this.viewControl.free();		
	},
	activeRowByRecord: function(record)
	{
		this.viewControl.setActiveRow(this.viewControl.getRowByRecord(record));	
	},
	broadRow:function(record)
	{
		this.viewControl.broadRow(record);	
	},
	editRow:function(record)
	{
		this.viewControl.editRow();	
	},
	setFieldValue:function(fieldIndex,fieldValue)
	{
		this.getDataSet();
		this.dataSet.setFieldValue(fieldIndex,fieldValue);
	},	
	setPriFieldValue:function(fieldName,fieldValue,fieldText)
	{		
		if (this.viewControl)
			this.viewControl.setFieldValue(fieldName,fieldValue,fieldText);	
	},	
	onresize:function()
	{
		if (document.body.offsetWidth>this.element.offsetWidth+50 ||document.body.offsetWidth<this.element.offsetWidth -10)
		{
			this.setWidth();
		}
	},
	setWidth:function(width)
	{
		
		if (!this.headControl)
			return;
		
		var gridWidth  = 0;
		
		if (this.getAutoSize()!="none")
		{
			this.element.style.width   = "100%";
			
		    gridWidth = document.body.offsetWidth-6-this.getRightPlate();
		}else
		{
			if (width)
				this.element.style.width = width;
			
			gridWidth = this.element.offsetWidth;
		}	   	
		
		if (gridWidth <=0)
			return;
		
		this.element.style.width   = gridWidth-2;
		this.headControl.setWidth(gridWidth);
		 var offWidth = this.viewControl.setWidth(gridWidth);
		 
		this.headControl.setWidth(offWidth);				
		if (this.bottomControl)
			this.bottomControl.setWidth(offWidth);
				 
		this.width = gridWidth;
	   
	    this.changeCellSize();
	},	
	changeCellSize:function()
	{
		
		var offsetWidth  = this.element.offsetWidth - this.headControl.getWidth();
		if (this.viewControl.isScroll)
			offsetWidth = offsetWidth - 20;
		
		if ( (this.headControl.getWidth()>this.viewControl.getWidth()||offsetWidth>0)&& this.headControl.getVisibleCount())
		{
			
			var spaceWidth = offsetWidth/this.headControl.getVisibleCount();
			if (window.isfirefox)
				spaceWidth  = spaceWidth -3;
			this.headControl.setSpaceWidth(spaceWidth);
			this.viewControl.changeFieldWidth(this.headControl);			
		}
		
	},
	setHeight:function(height)
	{
		if (this.getAutoSize().isEqual("client")||this.getAutoSize().isEqual("bottom"))
		{
			var absTop = this.viewControl.getAbsTop();
			if (this.bottomControl)
				absTop = absTop +30;
			
			var height = document.body.offsetHeight -absTop-this.getBottomPlate();
			if (height>0)
				this.viewControl.setHeight(height);
		}
	},	
	free:function()
	{
		if (this.dataSet)
			this.dataSet.unregTable(this.element);		
	}
	
});

Yacon.widget.grid.list.BaseGrid.events = {
	onChoiceRowClick:function(rowcheck){
		var checkName = rowcheck.getAttribute("id").split("choice_");
		var tabview =  document.getElementById("tabview_"+checkName[1]);
		if (tabview)
		{
			if(rowcheck.checked){
				//选中状态，循环查看列表是否选中确定全选是否选中
				var rowcount = document.getElementById("tabview_"+checkName[1]).rows.length ;
				var checkable = true ;
				for(var i=0;i<rowcount;i++){
					var rowObj = document.getElementById("tabview_"+checkName[1]).rows[i].cells[0].children[0].checked ;
					checkable = checkable && rowObj ;
				} 
				var checkObj = document.all("title_choice_"+checkName[1]);
				checkObj.checked = checkable ;
			}else{
				//取消全选状态
				var checkName = rowcheck.getAttribute("id").split("choice_");
				var checkObj = document.all("title_choice_"+checkName[1]);
				checkObj.checked = false ;
			}
			if (tabview.Control)
				tabview.Control.setActiveRow( Yacon.getTR(rowcheck))
		}
		
		
	},	
	onChoiceClick:function(titleCheck)
	{
		var checkName = titleCheck.getAttribute("id").split("choice_");
		
		var isChecked = titleCheck.checked;
		if (checkName.length == 2)
			var checkObj = document.all("data_choice_"+checkName[1]);
		
		if (!checkObj)
			return;
		
		if (checkObj.length >0)
		{
			for (var i=0; i< checkObj.length; i++)
			{
				var obj= checkObj.item(i);
				if (obj.type == "checkbox")
				{
					obj.checked = isChecked;
				}
			}
		}if (checkObj.type == "checkbox")
		{
			checkObj.checked = isChecked;
		}
	},
	onGridResize:function(grid)
	{
		if (!grid.Control)
			return;
		
		grid.Control.setWidth();
	}
};





